﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestWP.GameCore
{
    class Core
    {
        public int[,] ArrNumber { get; set; }
        public static int WidthLenght { get; set; }
        public List<int> Result { get; set; }
        public int Level { get; set; }


        public void initArr(int widthLenght,int level)
        {
            WidthLenght = widthLenght;
            Level = level;
            ArrNumber = new int[WidthLenght, WidthLenght];
            int k = 0;
            for (int i = 0; i < WidthLenght; i++)
            {
                for (int j = 0; j < WidthLenght; j++)
                {
                    ArrNumber[i, j] = k++;
                }
            }
        }

        public void rndArr()
        {
            Random rd = new Random();
            Result = new List<int>();
            int rnd;
            int xi = WidthLenght - 1, yi = WidthLenght - 1;
            for (int i = 0; i < Level; i++)
            {
                switch ((rnd = rd.Next(0, 3)))
                {
                    case 0:
                        if (swapUp(xi, yi))
                        {
                            xi -= 1;
                            Result.Add(rnd);
                        }
                        break;
                    case 1:
                        if (swapDown(xi, yi))
                        {
                            xi += 1;
                            Result.Add(rnd);
                        }
                        break;
                    case 2:
                        if (swapLeft(xi, yi))
                        {
                            yi -= 1;
                            Result.Add(rnd);
                        }
                        break;
                    case 3:
                        if (swapRight(xi, yi))
                        {
                            yi += 1;
                            Result.Add(rnd);
                        }
                        break;
                }
            }
        }

        public void swapArr(int fromx, int fromy, int tox, int toy)
        {
            int temp = ArrNumber[fromx, fromy];
            ArrNumber[fromx, fromy] = ArrNumber[tox, toy];
            ArrNumber[tox, toy] = temp;
        }

        public bool swapUp(int x, int y)
        {
            int temp = ArrNumber[x, y];
            if (x != 0)
            {
                ArrNumber[x, y] = ArrNumber[x - 1, y];
                ArrNumber[x - 1, y] = temp;
                return true;
            }
            return false;
        }
        public bool swapDown(int x, int y)
        {
            int temp = ArrNumber[x, y];
            if (x != (WidthLenght - 1))
            {
                ArrNumber[x, y] = ArrNumber[x + 1, y];
                ArrNumber[x + 1, y] = temp;
                return true;
            }
            return false;
        }
        public bool swapRight(int x, int y)
        {
            int temp = ArrNumber[x, y];
            if (y != (WidthLenght - 1))
            {
                ArrNumber[x, y] = ArrNumber[x, y + 1];
                ArrNumber[x, y + 1] = temp;
                return true;
            }
            return false;
        }
        public bool swapLeft(int x, int y)
        {
            int temp = ArrNumber[x, y];
            if (y != 0)
            {
                ArrNumber[x, y] = ArrNumber[x, y - 1];
                ArrNumber[x, y - 1] = temp;
                return true;
            }
            return false;
        }

        public string show()
        {
            string str = "";
            for (int i = 0; i < WidthLenght; i++)
            {
                for (int j = 0; j < WidthLenght; j++)
                {
                    str += ArrNumber[i, j] + " ";
                }
            }
            return str;
        }
    }
}
